<!-- subpkg_consult/order_list/components/order-list.vue -->
<script setup>
  import { getPatientConsultOrderListApi } from '@/services/consult'
  import { nextTick, ref } from 'vue'

  function throttle(fn, ms) {
    // 能不能调用
    let canCall = true
    return function (...args) {
      if (canCall) {
        canCall = false
        fn(...args)
        setTimeout(() => {
          canCall = true
        }, ms)
      }
    }
  }

  // // #ifdef H5
  // import { throttle } from 'licia'
  // // #endif

  // // #ifndef H5
  // import throttle from 'miniprogram-licia/throttle'
  // // #endif

  const props = defineProps({
    type: {
      type: String,
      default: '1',
    },
  })

  const list = ref([])
  const current = ref(1)
  const loadingStatus = ref('more') // more/loading/noMore
  const getList = async (isShowLoading) => {
    // 如果当前页码 已经大于 总页码了，就没必要发请求了
    if (loadingStatus.value === 'noMore') return

    loadingStatus.value = 'loading'

    const res = await getPatientConsultOrderListApi(
      {
        current: current.value,
        pageSize: 5,
        type: props.type,
      },
      isShowLoading
    )
    console.log(res, 'res')

    for (const v of res.data.rows) {
      v.__isShowMore = false
    }

    list.value.push(...res.data.rows)

    current.value++ // 该页还没有加载，++完后，是下一次分页加载的页码

    if (current.value > res.data.pageTotal) {
      loadingStatus.value = 'noMore'
    } else {
      loadingStatus.value = 'more'
    }
  }

  getList()

  const onScrolltolower = throttle(() => {
    getList()
  }, 300)

  const refresherTriggered = ref(false)
  const onRefresherrefresh = async () => {
    // 显示下拉效果
    refresherTriggered.value = true
    await nextTick()
    refresherTriggered.value = false

    // 1. 要清空当前数据列表
    list.value = []
    // 2. 要请求第一页的数据
    current.value = 1
    // 3. 重置加载中状态
    loadingStatus.value = 'more'
    await getList(false)

    // 收起下拉效果
    // refresherTriggered.value = false
  }
</script>

<template>
  <!-- :lower-threshold="0"   距底部/右边多远时（单位px），触发 scrolltolower 事件	 -->
  <!-- 1、监听下拉刷新的事件 -->
  <scroll-view
    refresher-enabled
    refresher-background="#f6f6f6"
    class="uni-scroll-view"
    scroll-y
    :refresher-triggered="refresherTriggered"
    @scrolltolower="onScrolltolower"
    @refresherrefresh="onRefresherrefresh"
  >
    <view class="consult-list">
      <view v-for="v of list" class="consult-list-item">
        <view class="consult-header">
          <view class="label">
            <image
              class="doctor-avatar"
              src="/static/uploads/doctor-avatar.jpg"
            />
            <text>{{ v.docInfo.name }}</text>
          </view>
          <text v-if="v.status === 1" class="status color-1">待支付</text>
          <text v-if="v.status === 2" class="status color-2">待接诊</text>
          <text v-if="v.status === 3" class="status color-2">咨询中</text>
          <text v-if="v.status === 4" class="status color-3">已完成</text>
          <text v-if="v.status === 5" class="status color-3">已取消</text>
        </view>
        <navigator
          class="consult-body"
          hover-class="none"
          url="/subpkg_consult/order_detail/index"
        >
          <uni-list :border="false">
            <uni-list-item
              :border="false"
              title="病情描述"
              :right-text="v.illnessDesc"
            />
            <uni-list-item
              :border="false"
              title="价格"
              :right-text="`¥ ${v.payment.toFixed(2)}`"
            />
            <uni-list-item
              :border="false"
              title="创建时间"
              :right-text="v.createTime"
            />
          </uni-list>
        </navigator>
        <view class="consult-footer">
          <view v-if="v.status === 1" class="uni-button more">
            <text class="label" @click="v.__isShowMore = !v.__isShowMore"
              >更多</text
            >
            <uni-transition :show="v.__isShowMore">
              <view class="list">
                <view class="list-item">查看处方</view>
                <view class="list-item">删除订单</view>
              </view>
            </uni-transition>
          </view>
          <view v-if="v.status === 1" class="consult-footer">
            <button class="uni-button minor">取消订单</button>
            <button class="uni-button">去支付</button>
          </view>
          <view v-if="v.status === 2" class="consult-footer">
            <button class="uni-button minor">取消订单</button>
            <button class="uni-button">继续咨询</button>
          </view>
          <view v-if="v.status === 3" class="consult-footer">
            <button class="uni-button">继续咨询</button>
          </view>
          <view v-if="v.status === 4" class="consult-footer">
            <button class="uni-button minor">问诊记录</button>
            <button v-if="v.evaluateId" class="uni-button minor">
              查看评价
            </button>
            <button v-else class="uni-button">去评价</button>
          </view>
          <view v-if="v.status === 5" class="consult-footer">
            <button class="uni-button minor">删除订单</button>
            <button class="uni-button">咨询其它医生</button>
          </view>
        </view>
      </view>

      <!-- 加载状态 -->
      <uni-load-more
        :status="loadingStatus"
        color="#C3C3C5"
        :icon-size="16"
        :content-text="{
          contentdown: '上拉显示更多',
          contentrefresh: '正在加载中',
          contentnomore: '我是有底线的~',
        }"
      />
    </view>
  </scroll-view>
</template>

<style lang="scss">
  // subpkg_consult/order_list/components/styles.scss
  .uni-scroll-view {
    height: calc(100vh - 50px);
    /* #ifdef H5 */
    height: calc(100vh - 94px);
    /* #endif */
    overflow: hidden;
    box-sizing: border-box;
    padding-top: 15rpx;
    padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  }

  .consult-list {
    padding: 15rpx 30rpx 30rpx;
  }

  .consult-list-item {
    // height: 715rpx;
    padding: 0 30rpx;
    margin-bottom: 30rpx;
    border-radius: 10rpx;
    background-color: #fff;

    .consult-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100rpx;
      padding: 0 30rpx;
      margin: 0 -30rpx;
      font-size: 30rpx;
      border-bottom: 1rpx solid #f5f5f5;

      .label {
        display: flex;
        align-items: center;
        color: #000;
        font-weight: 500;
      }

      .doctor-avatar {
        width: 40rpx;
        height: 40rpx;
        margin-right: 10rpx;
        border-radius: 50%;
      }

      .status {
        color: #121826;
      }

      .color-1 {
        color: #f2994a;
      }

      .color-2 {
        color: #16c2a3;
      }

      .color-3 {
        color: #848484;
      }
    }

    .consult-body {
      padding: 20rpx 0;
    }

    :deep(.uni-list-item__container) {
      padding: 10rpx 0 !important;
    }

    :deep(.uni-list-item__content-title) {
      font-size: 28rpx !important;
      color: #c3c3c5 !important;
    }

    :deep(.uni-list-item__extra-text) {
      font-size: 28rpx !important;
      color: #333 !important;
    }

    :deep(.uni-list-item__extra) {
      width: 480rpx !important;
      justify-content: flex-start !important;
    }

    .consult-footer {
      display: flex;
      justify-content: flex-end;
      padding: 30rpx 0;
      border-top: 1rpx solid #f5f5f5;

      .uni-button {
        height: 60rpx;
        line-height: 60rpx;
        /* #ifdef MP */
        line-height: 59rpx;
        /* #endif */
        margin: 0 0 0 30rpx;
        font-size: 26rpx;
        color: #16c2a3;
        border: 1rpx solid #16c2a3;
        background-color: #fff;

        &.more {
          flex: 1;
          text-align: left;
          margin-left: 0;
          border-color: transparent;
          color: #979797;
          position: relative;
        }

        .list {
          position: absolute;
          left: 0;
          bottom: 75rpx;
          z-index: 99;
          width: 160rpx;
          text-align: center;
          padding: 10rpx 30rpx;
          border-radius: 10rpx;
          font-size: 28rpx;
          box-shadow: 0 0 30rpx rgba(50, 50, 51, 0.3);
          background-color: #fff;

          &::after {
            position: absolute;
            bottom: -28rpx;
            left: 30rpx;
            content: '';
            border-width: 14rpx;
            border-style: solid;
            border-color: #fff transparent transparent transparent;
          }
        }

        .list-item {
          padding: 10rpx 0;
          border-bottom: 1rpx solid #eee;

          &:last-child {
            border-bottom: none;
          }
        }

        &.minor {
          color: #3c3e42;
          border-color: #ededed;
          background-color: #fafafa;
        }
      }
    }
  }
</style>
